package com.lianxian.xindong.login.mvp.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.android.library.YLCircleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.github.gzuliyujiang.dialog.DialogConfig;
import com.github.gzuliyujiang.dialog.DialogStyle;
import com.github.gzuliyujiang.wheelpicker.DatePicker;
import com.github.gzuliyujiang.wheelpicker.OptionPicker;
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode;
import com.github.gzuliyujiang.wheelpicker.contract.OnDatePickedListener;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionPickedListener;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionSelectedListener;
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity;
import com.github.gzuliyujiang.wheelpicker.impl.UnitDateFormatter;
import com.github.gzuliyujiang.wheelpicker.widget.DateWheelLayout;
import com.github.gzuliyujiang.wheelpicker.widget.OptionWheelLayout;
import com.github.gzuliyujiang.wheelview.annotation.CurtainCorner;
import com.hxb.library.base.BaseActivity;
import com.hxb.library.di.component.AppComponent;
import com.hxb.library.utils.AntiShakeUtils;
import com.hxb.library.utils.HxbUtils;
import com.lianxian.xindong.commonsdk.BuildConfig;
import com.lianxian.xindong.commonsdk.imgaEngine.config.CommonImageConfigImpl;
import com.lianxian.xindong.commonsdk.utils.ImmersionBarUtil;
import com.lianxian.xindong.login.R;
import com.lianxian.xindong.login.R2;
import com.lianxian.xindong.login.di.component.DaggerOtherStepOneComponent;
import com.lianxian.xindong.login.mvp.contract.OtherStepOneContract;
import com.lianxian.xindong.login.mvp.model.entity.JobBean;
import com.lianxian.xindong.login.mvp.presenter.OtherStepOnePresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.hxb.library.utils.Preconditions.checkNotNull;


/**
 * @作者：hexingbo
 * @时间：07/04/2023
 * @描述：
 */
public class OtherStepOneActivity extends BaseActivity<OtherStepOnePresenter> implements OtherStepOneContract.View {

    @BindView(R2.id.iv_head)
    YLCircleImageView ivHead;
    @BindView(R2.id.et_nice)
    EditText etNice;
    @BindView(R2.id.tv_birthday)
    TextView tvBirthday;
    @BindView(R2.id.tv_job)
    TextView tvJob;
    @BindView(R2.id.btn_submit)
    Button btnSubmit;

    private DatePicker mPickerBirthday;
    private OptionPicker mPickerJob;

    @Override
    protected void onDestroy() {
        mPickerBirthday = null;
        mPickerJob = null;
        super.onDestroy();
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerOtherStepOneComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_other_step_one; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        ImmersionBarUtil.setImmersionBar(this, true);
        btnSubmit.setText(R.string.login_next);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        HxbUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        HxbUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void setUserHeadImg(String url) {
        HxbUtils.obtainAppComponentFromContext(getContext())
                .imageLoader().loadImage(getContext(),
                CommonImageConfigImpl.builder()
                        .url(TextUtils.isEmpty(url) ? BuildConfig.DEFAULT_HEAD_URL : url)
                        .errorPic(R.drawable.ic_defunct_head)
                        .placeholder(R.drawable.ic_defunct_head)
                        .imageRadius(10)
                        .imageView(ivHead)
                        .build());
    }


    @Override
    public void showBirthdayDialog() {
        if (mPickerBirthday == null) {
            DialogConfig.setDialogStyle(DialogStyle.Three);
            mPickerBirthday = new DatePicker(this);
            //pickerBirthday.setBodyWidth(240);
            mPickerBirthday.setTitle("选择生日");
            DateWheelLayout wheelLayout = mPickerBirthday.getWheelLayout();
            wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY);
//        wheelLayout.setDateLabel("年", "月", "日");
            wheelLayout.setDateFormatter(new UnitDateFormatter());
            wheelLayout.setItemSpace(15);
            wheelLayout.setRange(DateEntity.target(1550, 1, 1), DateEntity.target(2050, 12, 31), DateEntity.today());
//        wheelLayout.setRange(DateEntity.today(), DateEntity.monthOnFuture(3));//选择时间区间范围
            wheelLayout.setCurtainEnabled(true);
            wheelLayout.setCurtainColor(getResources().getColor(R.color.public_color_transparent));//选中时间的背景色
            wheelLayout.setIndicatorEnabled(true);
            wheelLayout.setIndicatorColor(getResources().getColor(R.color.public_color_transparent));//滚轮指示器横线颜色
            wheelLayout.setIndicatorSize(getContext().getResources().getDisplayMetrics().density * 2);
            wheelLayout.setTextColor(getResources().getColor(R.color.public_color_gray));
            wheelLayout.setTextSize(14 * getResources().getDisplayMetrics().scaledDensity);
            //注：建议通过`setStyle`定制样式设置文字加大，若通过`setSelectedTextSize`设置，该解决方案会导致选择器展示时跳动一下
            //wheelLayout.setSelectedTextSize(16 * getResources().getDisplayMetrics().scaledDensity);
            wheelLayout.setSelectedTextColor(getResources().getColor(R.color.public_color_black));
            //wheelLayout.getYearLabelView().setTextColor(0xFF999999);
            //wheelLayout.getMonthLabelView().setTextColor(0xFF999999);
            mPickerBirthday.setOnDatePickedListener(new OnDatePickedListener() {
                @Override
                public void onDatePicked(int year, int month, int day) {
                    String birthday = year + "年" + month + "月" + day + "日";
                    tvBirthday.setText(birthday);
                    mPresenter.setBirthdayInfo(birthday);
                }
            });
            mPickerBirthday.getWheelLayout().setResetWhenLinkage(false);
        }
        mPickerBirthday.show();
    }

    @Override
    public void showJobDialog(List<JobBean> dataJob) {
        if (mPickerJob == null) {
            DialogConfig.setDialogStyle(DialogStyle.Three);
            mPickerJob = new OptionPicker(this);
            mPickerJob.setTitle("选择职业");
            mPickerJob.setBodyWidth(140);
            mPickerJob.setData(dataJob);
            mPickerJob.setDefaultPosition(2);
            mPickerJob.setOnOptionPickedListener(new OnOptionPickedListener() {
                @Override
                public void onOptionPicked(int position, Object item) {

                }
            });
            OptionWheelLayout wheelLayout = mPickerJob.getWheelLayout();
            wheelLayout.setIndicatorEnabled(false);

            wheelLayout.setTextColor(getResources().getColor(R.color.public_color_gray));
            wheelLayout.setTextSize(14 * getResources().getDisplayMetrics().scaledDensity);
            //注：建议通过`setStyle`定制样式设置文字加大，若通过`setSelectedTextSize`设置，该解决方案会导致选择器展示时跳动一下
            //wheelLayout.setSelectedTextSize(16 * getResources().getDisplayMetrics().scaledDensity);
            wheelLayout.setSelectedTextColor(getResources().getColor(R.color.public_color_black));
            wheelLayout.setSelectedTextSize(17 * getResources().getDisplayMetrics().scaledDensity);
            wheelLayout.setSelectedTextBold(true);
            wheelLayout.setCurtainEnabled(true);
            wheelLayout.setCurtainColor(getResources().getColor(R.color.public_color_transparent));//选中时间的背景色
            wheelLayout.setCurtainCorner(CurtainCorner.ALL);
            wheelLayout.setCurtainRadius(5 * getResources().getDisplayMetrics().density);
            wheelLayout.setOnOptionSelectedListener(new OnOptionSelectedListener() {
                @Override
                public void onOptionSelected(int position, Object item) {
                    String job = mPickerJob.getWheelView().formatItem(position);
                    tvJob.setText(job);
                    mPresenter.setJob(job);
                }
            });
        }
        mPickerJob.show();
    }


    @OnClick({R2.id.iv_head, R2.id.tv_birthday, R2.id.tv_job, R2.id.btn_submit})
    public void onViewClicked(View view) {
        if (AntiShakeUtils.isInvalidClick(view)) {
            return;
        }
        if (view.getId() == R.id.iv_head) {
            //选择头像
            mPresenter.chooseHeadPicture();
        } else if (view.getId() == R.id.tv_birthday) {
            //选择生日
            showBirthdayDialog();
        } else if (view.getId() == R.id.tv_job) {
            //选择职业
            mPresenter.getJobData();
        } else if (view.getId() == R.id.btn_submit) {
            //下一步
            mPresenter.goOtherStepTwo(etNice.getText().toString());
        }
    }


}
